package algorithms.leaning.class17;

import common.util.MyUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 打印一个字符串的全部排列，不用去重
 *
 * @author guichang
 * @date 2021/6/16
 */

@SuppressWarnings("all")
public class Code5_递归_字符串全排列_不去重 {

    public static void main(String[] args) {
        String str = "abcd";
        MyUtil.print(fullArray(str));
    }

    public static List<String> fullArray(String str) {
        List<String> list = new ArrayList<>();
        if (str == null || str.equals("")) {
            return list;
        }
        process(str.toCharArray(), 0, list);
        return list;
    }

    private static void process(char[] cs, int index, List<String> list) {
        // 到最后了，加入结果集
        if (index == cs.length) {
            list.add(new String(cs));
            return;
        }
        for (int i = index; i < cs.length; i++) {
            swap(cs, i, index);
            process(cs, index + 1, list);
            swap(cs, i, index);
        }
    }

    private static void swap(char[] cs, int i, int j) {
        char temp = cs[i];
        cs[i] = cs[j];
        cs[j] = temp;
    }

}